package com.simon.hse.activity.contractor;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.CommonInput;
import com.simon.hse.http.entity.ContractorCompanyOutput;
import com.simon.hse.http.entity.ContractorPersonInput;
import com.simon.hse.http.entity.ContractorPersonOutput;
import com.simon.hse.http.entity.TrainCourseOutput;
import com.simon.hse.http.entity.TrainPersonInput;
import com.simon.hse.http.entity.TrainPersonOutput;
import com.simon.hse.utils.DialogUtils;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit2.Call;

/**
 * 承包商管理
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_contractor_manager)
public class ContractorManagerActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("承包商管理");

        // 初始化承包商管理四个功能模块共享数据
//        initContractorCompany();
//        initCourseInfo();
        initTrainPerson();
        initParticipant();
    }

    @Event(value = {R.id.iv_common_left})
    private void back(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
        }
    }

    @Event(value = {R.id.ll_function_1, R.id.ll_function_2, R.id.ll_function_3, R.id.ll_function_4})
    private void clickFunction(View view) {
        switch (view.getId()) {
            case R.id.ll_function_1:
                startActivity(new Intent(ContractorManagerActivity.this, UtilsRecordActivity.class));
//                EnterOrExit();
                break;
            case R.id.ll_function_2:
                startActivity(new Intent(ContractorManagerActivity.this, TrainRecordActivity.class));
                break;
            case R.id.ll_function_3:
                startActivity(new Intent(ContractorManagerActivity.this, ExaminRecordActivity.class));
                break;
            case R.id.ll_function_4:
                startActivity(new Intent(ContractorManagerActivity.this, ViolationManagerActivity.class));
                break;
        }
    }

    /**
     * 入场/出场
     */
    private void EnterOrExit() {
        View inflate = LayoutInflater.from(ContractorManagerActivity.this).inflate(R.layout.layout_dialog_enter_or_exit, null);
        inflate.findViewById(R.id.iv_dialog_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.getInstance().exitDialog();
            }
        });
        inflate.findViewById(R.id.ll_dialog_enter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ContractorManagerActivity.this, EnterActivity.class));
                DialogUtils.getInstance().exitDialog();
            }
        });
        inflate.findViewById(R.id.ll_dialog_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ContractorManagerActivity.this, ExitActivity.class));
                DialogUtils.getInstance().exitDialog();
            }
        });
        DialogUtils.getInstance().createDialog(ContractorManagerActivity.this, inflate, Gravity.CENTER, 0.8, 0.0);
    }

    /**
     * 课件名称
     */
    private void initCourseInfo() {
        CommonInput commonInput = new CommonInput();
        CommonInput.ModelBean modelBean = new CommonInput.ModelBean();
        commonInput.setModel(modelBean);
        commonInput.setLimit(20);
        commonInput.setOffset(0);

        Call<String> call = HttpManager.getInstance().requestService().COURSE_INFO(HttpManager.getInstance().requestBody(commonInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                TrainCourseOutput trainCourseOutput= new Gson().fromJson(result, TrainCourseOutput.class);
                if (trainCourseOutput.getResult().equals("Success")) {
                    List<TrainCourseOutput.DataBean.PageDataBean> pageDataBeanList = trainCourseOutput.getData().getPageData();
                    MyApplication.getInstance().mListTrainCourse.clear();
                    MyApplication.getInstance().mListTrainCourse.addAll(pageDataBeanList);
                } else {
                    ToastUtils.getInstance().showShortToastBottom(trainCourseOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    /**
     * 供应商公司
     */
    private void initContractorCompany() {
        ContractorPersonInput contractorPersonInput = new ContractorPersonInput();
        ContractorPersonInput.ModelBean modelBean = new ContractorPersonInput.ModelBean();
        contractorPersonInput.setModel(modelBean);
        contractorPersonInput.setLimit(20);
        contractorPersonInput.setOffset(0);

        Call<String> call = HttpManager.getInstance().requestService().CONTRACTOR(HttpManager.getInstance().requestBody(contractorPersonInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                ContractorCompanyOutput contractorPersonOutput = new Gson().fromJson(result, ContractorCompanyOutput.class);
                if (contractorPersonOutput.getResult().equals("Success")) {
                    List<ContractorCompanyOutput.DataBean.PageDataBean> pageDataBeanList = contractorPersonOutput.getData().getPageData();
                    MyApplication.getInstance().mListContractorCompany.clear();
                    MyApplication.getInstance().mListContractorCompany.addAll(pageDataBeanList);
                } else {
                    ToastUtils.getInstance().showShortToastBottom(contractorPersonOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    /**
     * 培训人 承包商人员
     */
    private void initTrainPerson() {
        TrainPersonInput trainPersonInput = new TrainPersonInput();
        TrainPersonInput.ModelBean modelBean = new TrainPersonInput.ModelBean();
        TrainPersonInput.ModelBean.SysUserInforBean sysUserInforBean = new TrainPersonInput.ModelBean.SysUserInforBean();
        modelBean.setSysUserInfor(sysUserInforBean);
        trainPersonInput.setModel(modelBean);
        trainPersonInput.setLimit(20);
        trainPersonInput.setOffset(0);

        Call<String> call = HttpManager.getInstance().requestService().TRAIN_PERSON(HttpManager.getInstance().requestBody(trainPersonInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                TrainPersonOutput trainPersonOutput = new Gson().fromJson(result, TrainPersonOutput.class);
                if (trainPersonOutput.getResult().equals("Success")) {
                    List<TrainPersonOutput.DataBean.PageDataBean> pageDataBeanList = trainPersonOutput.getData().getPageData();
                    MyApplication.getInstance().mListTrainPerson.clear();
                    MyApplication.getInstance().mListTrainPerson.addAll(pageDataBeanList);
                } else {
                    ToastUtils.getInstance().showShortToastBottom(trainPersonOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    /**
     * 参与人 违章人
     */
    private void initParticipant() {
        CommonInput commonInput = new CommonInput();
        CommonInput.ModelBean modelBean = new CommonInput.ModelBean();
        commonInput.setModel(modelBean);
        commonInput.setLimit(20);
        commonInput.setOffset(0);

        Call<String> call = HttpManager.getInstance().requestService().CONTRACTOR_PERSON(HttpManager.getInstance().requestBody(commonInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                ContractorPersonOutput contractorPersonOutput = new Gson().fromJson(result, ContractorPersonOutput.class);
                if (contractorPersonOutput.getResult().equals("Success")) {
                    List<ContractorPersonOutput.DataBean.PageDataBean> pageDataBeanList = contractorPersonOutput.getData().getPageData();
                    MyApplication.getInstance().mListParticipant.clear();
                    MyApplication.getInstance().mListParticipant.addAll(pageDataBeanList);
                } else {
                    ToastUtils.getInstance().showShortToastBottom(contractorPersonOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }
}
